# -*- coding: utf-8 -*-

#from apyori import apriori

transactions = [    ['面包', '牛奶','香蕉'],
    ['面包', '牛奶', '鸡蛋'],
    ['牛奶', '鸡蛋','香蕉'],
    ['面包', '牛奶', '鸡蛋'],
    ['面包', '牛奶', '鸡蛋', '香蕉'],
    ['面包', '牛奶', '香蕉'],
    ['面包', '牛奶', '鸡蛋'],
    ['牛奶', '鸡蛋','香蕉'],
    ['面包', '牛奶', '鸡蛋']
]
'''
results = list(apriori(transactions, min_support=0.4))

for item in results:
    #pair = item[0] 
    #items = [x for x in pair]
    #print("频繁项集: " + str(items))
    #print("支持度: " + str(item[1]))
    #print("置信度: " + str(item[2][0][2]))
    #print("\n")
    print(item[2][0])


itemSetList = [['eggs', 'bacon', 'soup'],
                ['eggs', 'bacon', 'apple'],
                ['soup', 'bacon', 'banana']]
freqItemSet, rules = fpgrowth(itemSetList, minSupRatio=0.5, minConf=0.5)
print(rules)  
'''


def get_support(tar_list):
    res = 0

    for trans in transactions:
        flag = True
        for item in tar_list:
            if not item in trans:
                flag = False
                break
        if flag:
            res += 1
    return res

def get_support_plus(tar_list, tar_trans):
    
    res_list = []
    for trans in transactions:
        flag = True
        for item in tar_list:
            if not item in trans:
                flag = False
                break
        if flag:
            res_list.append(trans)
    return res_list


item_set = set()

for trans in transactions:
    for item in trans:
        item_set.add(item)

ITEM_LIST = [] 
ITEM_MAP = []


# minsup * len
MIN_SUPPORT = 4


for item in sorted(list(item_set)):
    temp = get_support_plus([item], transactions)
    if len(temp) >= MIN_SUPPORT:
        ITEM_LIST.append(item)
        ITEM_MAP.append(temp)
    

RES_LIST = []


def search_tree(cur_list, cur_trans):
    global RES_LIST
    last_item = cur_list[-1]
    pos = ITEM_LIST.index(last_item)

    for cur_item in ITEM_LIST[pos+1:]:
        temp = get_support_plus(cur_list + [cur_item], cur_trans)
        if len(temp) >= MIN_SUPPORT:
            RES_LIST.append(cur_list + [cur_item])
            search_tree(cur_list + [cur_item], temp)

for i in range(len(ITEM_LIST)):
    RES_LIST.append([ITEM_LIST[i]])
    search_tree([ITEM_LIST[i]], ITEM_MAP[i])

for item in RES_LIST:
    print(item)











